﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;

namespace DynaCAD
{
    /// <summary>
    /// Interaction logic for NewPartCreator.xaml
    /// </summary>
    public partial class NewPartCreator : Window
    {
        Color AktiveColor;
        string Aim="";
        string CurrentFilePath = "";
        Point? MovePointStackPanel = null;
        Point? MidPointStackPanel = null;
        System.Windows.Shapes.Path RemoveLine = new System.Windows.Shapes.Path();
        System.Windows.Shapes.Path RemoveArc = new System.Windows.Shapes.Path();
        System.Windows.Shapes.Path RemoveBezier = new System.Windows.Shapes.Path();
        System.Windows.Shapes.Path RemoveQuadraticBezier = new System.Windows.Shapes.Path();
        System.Windows.Shapes.Path RemoveSpline = new System.Windows.Shapes.Path();
        System.Windows.Shapes.Path RemoveRectangle = new System.Windows.Shapes.Path();
        System.Windows.Shapes.Path RemoveEllipse = new System.Windows.Shapes.Path();
        AdornerLayer ADL;
        UIElement SelectedUIElement = null;
        Canvas AktiveCanvas;
        int SelectIndex = 0;
        List<System.Windows.Shapes.Path> UnitePaths = new List<System.Windows.Shapes.Path>();
        List<System.Windows.Shapes.Path> PossibleSelectedPaths = new List<System.Windows.Shapes.Path>();
        List<Point> ClickedPoints = new List<Point>();
        List<PathMaker.Node> Nodes = new List<PathMaker.Node>();

        public NewPartCreator()
        {
            InitializeComponent();
            AktiveColor = Color.FromArgb((byte)AktiveColorA.Value, (byte)AktiveColorR.Value, (byte)AktiveColorG.Value, (byte)AktiveColorB.Value);
        }


        //File Operations
        private void Open_File_Click(object sender, RoutedEventArgs e)
        {
            Stream myStream = null;
            System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
            ofld.Filter = "svg files (*.svg)|*.svg|xaml files (*.xaml)|*.xaml|All files (*.*)|*.*";
            ofld.FilterIndex = 2;
            ofld.RestoreDirectory = true;
            if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    if ((myStream = ofld.OpenFile()) != null)
                    {
                        myStream.Close();
                            String FileName = ofld.FileName;
                            if(System.IO.Path.GetExtension(FileName)==".xaml")
                            {
                                using (System.IO.FileStream stream = new System.IO.FileStream(FileName, System.IO.FileMode.Open))
                                {
                                    try
                                    {
                                        Viewbox VB = new Viewbox();
                                        Canvas CV = new Canvas();
                                        UIElement UI = (UIElement)System.Windows.Markup.XamlReader.Load(stream);
                                        if (UI is Viewbox)
                                        {
                                            VB = (Viewbox)UI;
                                            CV = (Canvas)VB.Child;
                                        }
                                        if(UI is Canvas)
                                        {
                                            CV=(Canvas)UI;
                                        }
                                        AktiveCanvas = CV;
                                        CV.ClipToBounds = true;
                                        VB.Child = null;
                                        ParcaViewBox.Child = CV;
                                        ParcaViewBox.ClipToBounds = true;
                                        ParcaStackPanel.RenderTransform = null;
                                        if (CV.Width > CV.Height)
                                        {
                                            ParcaViewBox.Width = 600;
                                            ParcaViewBox.Height = 600 / (CV.Width / CV.Height);
                                            ParcaGrid.Width = ParcaViewBox.Width;
                                            ParcaGrid.Height = ParcaViewBox.Height;
                                            ParcaStackPanel.Width = ParcaViewBox.Width;
                                            ParcaStackPanel.Height = ParcaViewBox.Height;
                                        }
                                        else
                                        {
                                            if (CV.Width < CV.Height)
                                            {
                                                ParcaViewBox.Height = 600;
                                                ParcaViewBox.Width = 600 / (CV.Height / CV.Width);
                                                ParcaGrid.Width = ParcaViewBox.Width;
                                                ParcaGrid.Height = ParcaViewBox.Height;
                                                ParcaStackPanel.Width = ParcaViewBox.Width;
                                                ParcaStackPanel.Height = ParcaViewBox.Height;
                                            }
                                            else
                                            {
                                                ParcaViewBox.Width = 600;
                                                ParcaViewBox.Height = 600;
                                                ParcaGrid.Width = ParcaViewBox.Width;
                                                ParcaGrid.Height = ParcaViewBox.Height;
                                                ParcaStackPanel.Width = ParcaViewBox.Width;
                                                ParcaStackPanel.Height = ParcaViewBox.Height;
                                            }
                                        }
                                        DrawRect();
                                        XamlEditor.TreeViewEditor.TreeViewOfCanvas(CV, FileTree);
                                        CurrentFilePath = FileName;
                                        return;
                                    }
                                    catch { System.Windows.MessageBox.Show("Dosya Açılamadı"); }
                                }
                                return;
                            }
                            else if (System.IO.Path.GetExtension(FileName) == ".svg")
                            {
                                Viewbox VB = new Viewbox();
                                Canvas CV=new Canvas();
                                VB=SP1.Convertor.Svg2Viewbox(FileName);
                                CV = (Canvas)VB.Child;
                                AktiveCanvas = CV;
                                CV.ClipToBounds = true;
                                VB.Child = null;
                                ParcaViewBox.Child = CV;
                                ParcaViewBox.ClipToBounds = true;
                                ParcaStackPanel.RenderTransform = null;
                                if (CV.Width > CV.Height)
                                {
                                    ParcaViewBox.Width = 600;
                                    ParcaViewBox.Height = 600 / (CV.Width / CV.Height);
                                    ParcaGrid.Width = ParcaViewBox.Width;
                                    ParcaGrid.Height = ParcaViewBox.Height;
                                    ParcaStackPanel.Width = ParcaViewBox.Width;
                                    ParcaStackPanel.Height = ParcaViewBox.Height;
                                }
                                else 
                                {
                                    if (CV.Width < CV.Height)
                                     {
                                      ParcaViewBox.Height = 600;
                                      ParcaViewBox.Width = 600 / (CV.Height / CV.Width);
                                      ParcaGrid.Width = ParcaViewBox.Width;
                                      ParcaGrid.Height = ParcaViewBox.Height;
                                      ParcaStackPanel.Width = ParcaViewBox.Width;
                                      ParcaStackPanel.Height = ParcaViewBox.Height;
                                     }
                                     else
                                     {
                                         ParcaViewBox.Width = 600;
                                         ParcaViewBox.Height = 600;
                                         ParcaGrid.Width = ParcaViewBox.Width;
                                         ParcaGrid.Height = ParcaViewBox.Height;
                                         ParcaStackPanel.Width = ParcaViewBox.Width;
                                         ParcaStackPanel.Height = ParcaViewBox.Height;
                                     }
                                }
                                DrawRect();
                                XamlEditor.TreeViewEditor.TreeViewOfCanvas(CV,FileTree);
                                CurrentFilePath = FileName;
                                return;
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("Desteklenmeyen Dosya Formatı");
                            }        
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
        }

        private void New_File_Click(object sender, RoutedEventArgs e)
        {
            New_File FL = new New_File();
            FL.ShowDialog();
        }

        private void File_SaveAs_Click(object sender, RoutedEventArgs e)
        {
            FileSaveAs();
        }

        private void File_Save_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentFilePath == "")
            {
                FileSaveAs();
                return;
            }
            else
            {
                FileSave();
                return;
            }
        }

        private void FileSaveAs()
        {
            System.Windows.Forms.SaveFileDialog sfld = new System.Windows.Forms.SaveFileDialog();
            sfld.DefaultExt = ".xaml";
            sfld.Filter = "xaml file (*.xaml)|*.xaml";

            if (sfld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (ParcaViewBox.Child != null)
                {
                    System.IO.FileStream stream = new System.IO.FileStream(sfld.FileName, System.IO.FileMode.Create);
                    // System.Windows.MessageBox.Show(sfld.FileName);
                    System.Windows.Markup.XamlWriter.Save(ParcaViewBox, stream);
                    CurrentFilePath = sfld.FileName;
                    stream.Close();
                }
                else
                {
                    System.Windows.MessageBox.Show("Nothing to Save");
                }
            }
        }

        private void FileSave()
        {
            if(System.IO.Path.GetExtension(CurrentFilePath) != ".xaml")
            {
                FileSaveAs();
            }
            else if (ParcaViewBox.Child != null)
            {
                System.IO.File.WriteAllText(CurrentFilePath, string.Empty);
                System.IO.FileStream stream = new System.IO.FileStream(CurrentFilePath, System.IO.FileMode.OpenOrCreate);
                // System.Windows.MessageBox.Show(sfld.FileName);
                System.Windows.Markup.XamlWriter.Save(ParcaViewBox, stream);
                stream.Close();
                System.Windows.MessageBox.Show("File Has Saved");
            }
            else
            {
                System.Windows.MessageBox.Show("Nothing to Save");
            }
        }

        public void CreateNewFile(double Width, double Height)
        {
            Canvas CV = new Canvas();
            AktiveCanvas = CV;
            CV.Width = Width;
            CV.Height = Height;
            CV.ClipToBounds = true;
            ParcaViewBox.Child = CV;
            ParcaViewBox.ClipToBounds = true;
            ParcaStackPanel.RenderTransform = null;
            if (CV.Width > CV.Height)
            {
                ParcaViewBox.Width = 600;
                ParcaViewBox.Height = 600 / (CV.Width / CV.Height);
                ParcaGrid.Width = ParcaViewBox.Width;
                ParcaGrid.Height = ParcaViewBox.Height;
                ParcaStackPanel.Width = ParcaViewBox.Width;
                ParcaStackPanel.Height = ParcaViewBox.Height;
            }
            else
            {
                if (CV.Width < CV.Height)
                {
                    ParcaViewBox.Height = 600;
                    ParcaViewBox.Width = 600 / (CV.Height / CV.Width);
                    ParcaGrid.Width = ParcaViewBox.Width;
                    ParcaGrid.Height = ParcaViewBox.Height;
                    ParcaStackPanel.Width = ParcaViewBox.Width;
                    ParcaStackPanel.Height = ParcaViewBox.Height;
                }
                else
                {
                    ParcaViewBox.Width = 600;
                    ParcaViewBox.Height = 600;
                    ParcaGrid.Width = ParcaViewBox.Width;
                    ParcaGrid.Height = ParcaViewBox.Height;
                    ParcaStackPanel.Width = ParcaViewBox.Width;
                    ParcaStackPanel.Height = ParcaViewBox.Height;
                }
            }
            DrawRect();
            XamlEditor.TreeViewEditor.TreeViewOfCanvas(CV, FileTree);
            return;
        }

        private void DynaCAD_Editor_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            foreach (Window a in Application.Current.Windows)
            {
                try
                {
                    a.Visibility = Visibility.Visible;
                }
                catch { }
            }
        }
        

        //Select Scale Translate and Mouse Events
        private void TranslateStackPanel(StackPanel x, System.Windows.Input.MouseEventArgs e)
        {
            if (MidPointStackPanel != null)
            {
                if (e.MiddleButton == MouseButtonState.Pressed)
                {
                    Point firstpoint = e.GetPosition(MainGrid);
                    Double X;
                    Double Y;
                    X = firstpoint.X - MidPointStackPanel.Value.X;
                    Y = firstpoint.Y - MidPointStackPanel.Value.Y;
                    GeneralTransform GTGrid = ParcaGrid.TransformToVisual(MainGrid);
                    GeneralTransform GTx = x.TransformToVisual(MainGrid);
                    Point Grid = GTGrid.Transform(new Point(0, 0));
                    Point Stack = GTx.Transform(new Point(0, 0));
                    Point GridL = GTGrid.Transform(new Point(ParcaGrid.Width, ParcaGrid.Height));
                    Point StackL = GTx.Transform(new Point(x.Width, x.Height));
                    if (X > 0 && X > (Grid.X - Stack.X))
                    {
                        X = Grid.X - Stack.X;
                    }
                    if (Y > 0 && Y > (Grid.Y - Stack.Y))
                    {
                        Y = Grid.Y - Stack.Y;
                    }
                    if (X < 0 && X < (GridL.X - StackL.X))
                    {
                        X = GridL.X - StackL.X;
                    }
                    if (Y < 0 && Y < (GridL.Y - StackL.Y))
                    {
                        Y = GridL.Y - StackL.Y;
                    }
                    if (x.RenderTransform is TransformGroup)
                    {
                        if (((TransformGroup)(x.RenderTransform)).Children.OfType<TranslateTransform>().Count<TranslateTransform>() > 0)
                        {
                            TranslateTransform TS = ((TransformGroup)(x.RenderTransform)).Children.OfType<TranslateTransform>().First<TranslateTransform>();
                            TS.X = TS.X + X;
                            TS.Y = TS.Y + Y;
                            MidPointStackPanel = e.GetPosition(MainGrid);
                        }
                        else
                        {
                            TranslateTransform TS = new TranslateTransform();
                            TS.X = X;
                            TS.Y = Y;
                            ((TransformGroup)(x.RenderTransform)).Children.Add(TS);
                        }
                    }
                    else
                    {
                        TransformGroup TG = new TransformGroup();
                        TranslateTransform TS = new TranslateTransform();
                        TS.X = X;
                        TS.Y = Y;
                        TG.Children.Add(TS);
                        x.RenderTransform = TG;
                    }
                }
                else
                {
                    MidPointStackPanel = e.GetPosition(MainGrid);
                }
                DrawRect();
            }
            else
            {
                MidPointStackPanel = e.GetPosition(MainGrid);
            }
        }

        private void ScaleStackPanel(StackPanel x, MouseWheelEventArgs e)
        {
            if (x.RenderTransform is TransformGroup)
            {
                if (((TransformGroup)(x.RenderTransform)).Children.OfType<ScaleTransform>().Count<ScaleTransform>() > 0)
                {
                    ScaleTransform ST = ((TransformGroup)(x.RenderTransform)).Children.OfType<ScaleTransform>().First<ScaleTransform>();
                    if (e.Delta > 0)
                    {
                        ST.ScaleX = ST.ScaleX * 1.1;
                        ST.ScaleY = ST.ScaleY * 1.1;
                        ST.CenterX = e.GetPosition(ParcaStackPanel).X;
                        ST.CenterY = e.GetPosition(ParcaStackPanel).Y;
                        if (x.RenderTransform is TransformGroup)
                            if (((TransformGroup)(x.RenderTransform)).Children.OfType<TranslateTransform>().Count<TranslateTransform>() > 0)
                            {
                                TranslateTransform TS = ((TransformGroup)(x.RenderTransform)).Children.OfType<TranslateTransform>().First<TranslateTransform>();
                                TS.X = 0;
                                TS.Y = 0;
                            }
                    }
                    if (e.Delta < 0)
                    {

                        ST.ScaleX = ST.ScaleX * 0.9;
                        ST.ScaleY = ST.ScaleY * 0.9;
                        ST.CenterX = e.GetPosition(ParcaStackPanel).X;
                        ST.CenterY = e.GetPosition(ParcaStackPanel).Y;
                        if (ST.ScaleX < 1)
                        {
                            ST.ScaleX = 1;
                            ST.ScaleY = 1;
                        }
                        if (x.RenderTransform is TransformGroup)
                            if (((TransformGroup)(x.RenderTransform)).Children.OfType<TranslateTransform>().Count<TranslateTransform>() > 0)
                            {
                                TranslateTransform TS = ((TransformGroup)(x.RenderTransform)).Children.OfType<TranslateTransform>().First<TranslateTransform>();
                                TS.X = 0;
                                TS.Y = 0;
                            }
                    }
                }
                else
                {
                    ScaleTransform ST = new ScaleTransform();
                    if (e.Delta > 0)
                    {
                        ST.ScaleX = 1.1;
                        ST.ScaleY = 1.1;
                        ST.CenterX = e.GetPosition(ParcaStackPanel).X;
                        ST.CenterY = e.GetPosition(ParcaStackPanel).Y;
                    }
                    if (e.Delta < 0)
                    {
                        ST.ScaleX = 1;
                        ST.ScaleY = 1;
                        ST.CenterX = e.GetPosition(ParcaStackPanel).X;
                        ST.CenterY = e.GetPosition(ParcaStackPanel).Y;
                    }
                    ((TransformGroup)(x.RenderTransform)).Children.Insert(0, ST);

                }
            }
            else
            {
                TransformGroup TG = new TransformGroup();
                ScaleTransform ST = new ScaleTransform();
                if (e.Delta > 0)
                {
                    ST.ScaleX = 1.1;
                    ST.ScaleY = 1.1;
                    ST.CenterX = e.GetPosition(ParcaStackPanel).X;
                    ST.CenterY = e.GetPosition(ParcaStackPanel).Y;
                }
                if (e.Delta < 0)
                {
                    ST.ScaleX = 1;
                    ST.ScaleY = 1;
                    ST.CenterX = e.GetPosition(ParcaStackPanel).X;
                    ST.CenterY = e.GetPosition(ParcaStackPanel).Y;
                }
                TG.Children.Insert(0, ST);
                x.RenderTransform = TG;
            }
            DrawRect();
        }

        private void Selected_ItemIndex(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            string indexString = null;
            List<int> i = new List<int>();
            UIElement A = (UIElement)ParcaViewBox.Child;
            try
            {
                indexString = ((TreeViewItem)(FileTree.SelectedItem)).Name;
            }
            catch { return; }
            indexString.Replace("A", "0").Split('_').OfType<string>().ToList<string>().ForEach(a =>
            {
                i.Add(Convert.ToInt32(a));
            });
            for (int a = 0; a < i.Count - 1; ++a)
            {
                A = (UIElement)(((Canvas)(A)).Children[i[a + 1]]);
            }
            try
            {
                if (SelectedUIElement != null)
                {
                    Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                    if (toRemoveArray != null)
                    {
                        for (int x = 0; x < toRemoveArray.Length; x++)
                        {
                            ADL.Remove(toRemoveArray[x]);
                        }
                    }
                }
            }
            catch { }
            SelectedUIElement = A;
            if (SelectedUIElement is Canvas)
            {
                AktiveCanvas = (Canvas)(SelectedUIElement);
            }
            else if (Convert.ToString(A.GetType()) == "System.Windows.Shapes.Path")
            {
                AktiveCanvas = ((Canvas)(((System.Windows.Shapes.Path)(A)).Parent));
                if (((System.Windows.Shapes.Path)A).Data is StreamGeometry)
                {
                    ((System.Windows.Shapes.Path)A).Data = ((System.Windows.Shapes.Path)A).Data.GetFlattenedPathGeometry().Clone();
                }

                ADL = AdornerLayer.GetAdornerLayer(A);
                ADL.Add(new XamlEditor.ResizingAdorner((System.Windows.Shapes.Path)A));
            }
        }

        private void DrawRect()
        {
            GeneralTransform GTGrid = ParcaGrid.TransformToVisual(MainGrid);
            GeneralTransform GTx = ParcaStackPanel.TransformToVisual(MainGrid);
            Point Grid = GTGrid.Transform(new Point(0, 0));
            Point Stack = GTx.Transform(new Point(0, 0));
            Point GridL = GTGrid.Transform(new Point(ParcaGrid.Width, ParcaGrid.Height));
            Point StackL = GTx.Transform(new Point(ParcaStackPanel.Width, ParcaStackPanel.Height));
            double BaseTop = -200;
            double BaseLeft = 0;
            double BaseRight = 0;
            double BaseBottom = 0;
            double RealTop;
            double RealLeft;
            double RealRight;
            double RealBottom;
            if (ParcaStackPanel.Width > ParcaStackPanel.Height)
            {
                BaseTop = -200 + ((200 - (200 / (ParcaStackPanel.Width / ParcaStackPanel.Height))) / 2);
                BaseBottom = (200 - (200 / (ParcaStackPanel.Width / ParcaStackPanel.Height))) / 2;
            }
            else
            {
                BaseLeft = (200 - (200 / (ParcaStackPanel.Height / ParcaStackPanel.Width))) / 2;
                BaseRight = (200 - (200 / (ParcaStackPanel.Height / ParcaStackPanel.Width))) / 2;
            }
            RealLeft = BaseLeft + (Stack.X - Grid.X) * ((200 - BaseLeft - BaseRight) / (Stack.X - StackL.X));
            RealRight = BaseRight + (GridL.X - StackL.X) * ((200 - BaseLeft - BaseRight) / (Stack.X - StackL.X));
            RealTop = BaseTop + ((Stack.Y - Grid.Y) * ((-BaseTop - BaseBottom) / (Stack.Y - StackL.Y)));
            RealBottom = BaseBottom + (GridL.Y - StackL.Y) * ((-BaseTop - BaseBottom) / (Stack.Y - StackL.Y));
            ViewRect.Margin = new Thickness(RealLeft, RealTop, RealRight, RealBottom);
        }

        private void ParcaStackPanel_Mouse_Leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            MidPointStackPanel = null;
            MovePointStackPanel = null;
        }

        private void ParcaStackPanel_Mouse_Move(object sender, System.Windows.Input.MouseEventArgs e)
        {
            MovePointStackPanel = e.GetPosition(AktiveCanvas);
            TranslateStackPanel(ParcaStackPanel, e);
            if (Aim != ""&&ClickedPoints.Count!=0)
            {
                switch (Aim)
                {
                    case "Line": 
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveLine);
                                }
                                catch { }
                                LineGeometry LG = new LineGeometry(ClickedPoints[0], (Point)MovePointStackPanel);
                                RemoveLine.Data = LG;
                                RemoveLine.Stroke = System.Windows.Media.Brushes.Black;
                            }
                        } return;
                    case "PolyLine":
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveLine);
                                }
                                catch { }
                                LineGeometry LG = new LineGeometry(ClickedPoints[0], (Point)MovePointStackPanel);
                                RemoveLine.Data = LG;
                                RemoveLine.Stroke = System.Windows.Media.Brushes.Black;
                            }
                            else
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveLine);
                                }
                                catch { }
                                LineGeometry LG = new LineGeometry(ClickedPoints[ClickedPoints.Count-1], (Point)MovePointStackPanel);
                                RemoveLine.Data = LG;
                                RemoveLine.Stroke = System.Windows.Media.Brushes.Black;
                            }
              
                        }return;
                    case "Arc":
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveLine);
                                }
                                catch { }
                                LineGeometry LG = new LineGeometry(ClickedPoints[0], (Point)MovePointStackPanel);
                                RemoveLine.Data = LG;
                                RemoveLine.Stroke = System.Windows.Media.Brushes.Black;
                                return;
                            }
                            if (ClickedPoints.Count == 2)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveArc);
                                }
                                catch { }
                                List<Point> a=new List<Point>();
                                a.Add(ClickedPoints[0]);
                                a.Add(ClickedPoints[1]);
                                a.Add((Point)MovePointStackPanel);
                                System.Windows.Shapes.Path x = XamlEditor.PathCreator.ArcCreater(a);
                                AktiveCanvas.Children.Remove(RemoveLine);
                                RemoveArc.Data = x.Data;
                                Canvas.SetLeft(RemoveArc, Canvas.GetLeft(x));
                                Canvas.SetTop(RemoveArc, Canvas.GetTop(x));
                                RemoveArc.Stroke = System.Windows.Media.Brushes.Black;
                            }
                        
                        }return;
                    case "Bezier":
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveLine);
                                }
                                catch { }
                                LineGeometry LG = new LineGeometry(ClickedPoints[0], (Point)MovePointStackPanel);
                                RemoveLine.Data = LG;
                                RemoveLine.Stroke = System.Windows.Media.Brushes.Black;
                                return;
                            }
                            if (ClickedPoints.Count == 2)
                            {
                                AktiveCanvas.Children.Remove(RemoveLine);
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveBezier);
                                }
                                catch { }
                                List<Point> a = new List<Point>();
                                a.Add(ClickedPoints[0]);
                                a.Add(ClickedPoints[1]);
                                a.Add((Point)MovePointStackPanel);
                                a.Add((Point)MovePointStackPanel);
                                System.Windows.Shapes.Path x = XamlEditor.PathCreator.BezierCreater(a);
                                AktiveCanvas.Children.Remove(RemoveLine);
                                RemoveBezier.Data = x.Data;
                                RemoveBezier.Stroke = System.Windows.Media.Brushes.Black;
                                return;

                            }
                            if (ClickedPoints.Count == 3)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveBezier);
                                }
                                catch { }
                                List<Point> a = new List<Point>();
                                a.Add(ClickedPoints[0]);
                                a.Add(ClickedPoints[1]);
                                a.Add(ClickedPoints[2]);
                                a.Add((Point)MovePointStackPanel);
                                System.Windows.Shapes.Path x = XamlEditor.PathCreator.BezierCreater(a);
                                AktiveCanvas.Children.Remove(RemoveLine);
                                RemoveBezier.Data = x.Data;
                                RemoveBezier.Stroke = System.Windows.Media.Brushes.Black;
                            }
                        
                        }return;
                    case "QuadraticBezier":
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveLine);
                                }
                                catch { }
                                LineGeometry LG = new LineGeometry(ClickedPoints[0], (Point)MovePointStackPanel);
                                RemoveLine.Data = LG;
                                RemoveLine.Stroke = System.Windows.Media.Brushes.Black;
                                return;
                            }
                            if (ClickedPoints.Count == 2)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveQuadraticBezier);
                                }
                                catch { }
                                List<Point> a = new List<Point>();
                                a.Add(ClickedPoints[0]);
                                a.Add(ClickedPoints[1]);
                                a.Add((Point)MovePointStackPanel);
                                AktiveCanvas.Children.Remove(RemoveLine);
                                System.Windows.Shapes.Path x = XamlEditor.PathCreator.QuadraticBezierCreater(a);
                                RemoveQuadraticBezier.Data = x.Data;
                                RemoveQuadraticBezier.Stroke = System.Windows.Media.Brushes.Black;
                            }
                        
                        }return;
                    case "Spline":
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                if (Mouse.LeftButton == MouseButtonState.Pressed)
                                {
                                    Nodes[Nodes.Count - 1].Nodetype = PathMaker.Node.NodeType.Symetrical;
                                    Nodes[Nodes.Count - 1].ControlPoint2 = e.GetPosition(AktiveCanvas);
                                }
                                PathMaker.Node A = new PathMaker.Node(NP: e.GetPosition(AktiveCanvas));
                                List<PathMaker.Node> LN = new List<PathMaker.Node>();
                                foreach (PathMaker.Node B in Nodes)
                                {
                                    LN.Add(B);
                                }
                                LN.Add(A);
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveSpline);
                                }
                                catch { }
                                RemoveSpline.Data = PathMaker.Node.GetPath(LN);
                                RemoveSpline.Stroke = System.Windows.Media.Brushes.Black;
                                return;
                            }
                            else
                            {
                                if (Mouse.LeftButton == MouseButtonState.Pressed)
                                {
                                    Nodes[Nodes.Count - 1].Nodetype = PathMaker.Node.NodeType.Symetrical;
                                    Nodes[Nodes.Count - 1].ControlPoint2 = e.GetPosition(AktiveCanvas);

                                }
                                List<Point> a = new List<Point>();
                                foreach (Point p in ClickedPoints)
                                    a.Add(p);
                                a.Add((Point)MovePointStackPanel);
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveSpline);
                                }
                                catch { }
                                
                                    PathMaker.Node A = new PathMaker.Node(NP: e.GetPosition(AktiveCanvas));
                                    List<PathMaker.Node> LN=new List<PathMaker.Node>();
                                    foreach (PathMaker.Node B in Nodes)
                                    {
                                        LN.Add(B);
                                    }
                                    LN.Add(A);
                                
                                //RemoveSpline.Data = XamlEditor.PathCreator.SplineCreater(a).Data;
                                RemoveSpline.Data = PathMaker.Node.GetPath(LN);
                                RemoveSpline.Stroke = System.Windows.Media.Brushes.Black;
                            }
                        }return;
                    case "Rectangle":
                        {
                            if (ClickedPoints.Count == 1)
                            {
                                try
                                {
                                    AktiveCanvas.Children.Add(RemoveRectangle);
                                }
                                catch { }
                                List<Point> a = new List<Point>();
                                a.Add(ClickedPoints[0]);
                                a.Add((Point)MovePointStackPanel);
                                RemoveRectangle.Data = XamlEditor.PathCreator.RectangleCreater(a).Data;
                                RemoveRectangle.Stroke = System.Windows.Media.Brushes.Black;
                            }
                        }return;
                    case "Ellipse":
                            {
                                if (ClickedPoints.Count == 1)
                                {
                                    try
                                    {
                                        AktiveCanvas.Children.Add(RemoveEllipse);
                                    }
                                    catch { }
                                    List<Point> a = new List<Point>();
                                    a.Add(ClickedPoints[0]);
                                    a.Add((Point)MovePointStackPanel);
                                    System.Windows.Shapes.Path x=new System.Windows.Shapes.Path();
                                    x = XamlEditor.PathCreator.EllipseCreater(a);
                                    RemoveEllipse.Stroke = System.Windows.Media.Brushes.Black;
                                    Canvas.SetLeft(RemoveEllipse,Canvas.GetLeft(x));
                                    Canvas.SetTop(RemoveEllipse,Canvas.GetTop(x));
                                    RemoveEllipse.Data=x.Data;
                                }
                            
                            }return;
                }
            
            }
        }

        private void ParcaStackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Aim == "Spline")
            {
                CreateSpline();
            }
        }

        private void ParcaStackPanel_Mouse_Wheel(object sender, MouseWheelEventArgs e)
        {
            ScaleStackPanel(ParcaStackPanel, e);
            if(ADL!=null)
            ADL.InvalidateVisual();
        }

        private void ParsaStackPanel_Mouse_Left_Button_Down(object sender, MouseButtonEventArgs e)
        {
            if (Aim != "")
            {
                ClickedPoints.Add(e.GetPosition(AktiveCanvas));
                switch (Aim)
                {
                    case "Line": { CreateLine(); } return;
                    case "PolyLine": { CreatePolyLine(); } return;
                    case "Arc": { CreateArc(); } return;
                    case "Bezier": { CreateBezier(); } return;
                    case "QuadraticBezier": { CreateQuadraticBezier(); } return;
                    case "Spline": 
                        {
                        Nodes.Add(new PathMaker.Node(NP: e.GetPosition(AktiveCanvas)));
                        } return;
                    case "Rectangle": { CreateRectangle(); } return;
                    case "Ellipse": { CreateEllipse(); } return;
                    case "Unite": { SelectPath(); } return;
                    case "Substract": { SelectPath(); } return;
                }
            }
            else
            {
                ClickedPoints.Add(e.GetPosition(AktiveCanvas));
                SelectPath();
            }
        }

        private void SelectPath()
        {
            Point ClickPoint = ClickedPoints[ClickedPoints.Count - 1];
            ClickedPoints.Clear();
            Rect Rec = new Rect(new Point(ClickPoint.X - 5, ClickPoint.Y - 5), new Point(ClickPoint.X + 5, ClickPoint.Y + 5));
            PossibleSelectedPaths.Clear();
            AktiveCanvas.Children.OfType<System.Windows.Shapes.Path>().ToList<System.Windows.Shapes.Path>().ForEach
                (p=>
                {
                    if (((Rect)(p.TransformToVisual(AktiveCanvas).TransformBounds(p.Data.Bounds))).IntersectsWith(Rec))
                    {
                        PossibleSelectedPaths.Add(p);
                    }
                });
            if (PossibleSelectedPaths.Count > 0)
            {
                try
                {
                    if (SelectedUIElement != null)
                    {
                        Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                        if (toRemoveArray != null)
                        {
                            for (int x = 0; x < toRemoveArray.Length; x++)
                            {
                                ADL.Remove(toRemoveArray[x]);
                            }
                        }
                    }
                }
                catch { }
                SelectedUIElement = PossibleSelectedPaths[0];
                if (((System.Windows.Shapes.Path)SelectedUIElement).Data is StreamGeometry)
                {
                    ((System.Windows.Shapes.Path)SelectedUIElement).Data = ((System.Windows.Shapes.Path)SelectedUIElement).Data.GetFlattenedPathGeometry().Clone();
                }
                ADL = AdornerLayer.GetAdornerLayer(SelectedUIElement);
                ADL.Add(new XamlEditor.ResizingAdorner((System.Windows.Shapes.Path)SelectedUIElement));
            }
            else
            {
                try
                {
                    if (SelectedUIElement != null)
                    {
                        Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                        if (toRemoveArray != null)
                        {
                            for (int x = 0; x < toRemoveArray.Length; x++)
                            {
                                ADL.Remove(toRemoveArray[x]);
                            }
                        }
                    }
                }
                catch { }
                SelectedUIElement = null;
            }
        }


        //KeyBordKeys//
        private void MainWindowKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape: { EscapeKey(); return; }
                case Key.Delete: { DeleteKey(); return; }
                case Key.Tab: { TabKey(); return; }
                case Key.Enter: { EnterKey(); return; }
            }
        }

        private void EnterKey()
        {
            if (SelectedUIElement is System.Windows.Shapes.Path && (Aim=="Unite" || Aim=="Substract"))
            {
                UnitePaths.Add((System.Windows.Shapes.Path)SelectedUIElement);
            }
            if (UnitePaths.Count == 2)
            {
                CombinedGeometry GG = new CombinedGeometry();
                GG.Geometry1 = UnitePaths[0].Data;
                GG.Geometry2 = UnitePaths[1].Data;
                switch(Aim)
                {
                    case "Unite": { GG.GeometryCombineMode = GeometryCombineMode.Union; break; }
                    case "Substract": { GG.GeometryCombineMode = GeometryCombineMode.Exclude; break; }
                }


                UnitePaths[0].Data = GG;
                GG.Geometry1.Transform = UnitePaths[0].RenderTransform;
                GG.Geometry2.Transform = UnitePaths[1].RenderTransform;
               // UnitePaths[0].RenderTransform = null;
                double d1=Canvas.GetLeft(UnitePaths[0]);
                double d2 = Canvas.GetTop(UnitePaths[0]);
                if (!(double.IsNaN(d1)))
                {
                    TranslateTransform TT = new TranslateTransform(d1, d2);
                   // System.Windows.MessageBox.Show(Convert.ToString(GG.Geometry2.Transform.GetType()));
                    if (GG.Geometry1.Transform is TranslateTransform)
                    {
                        ((TranslateTransform)(GG.Geometry1.Transform)).X = ((TranslateTransform)(GG.Geometry1.Transform)).X + d1;
                        ((TranslateTransform)(GG.Geometry1.Transform)).Y = ((TranslateTransform)(GG.Geometry1.Transform)).Y + d2;
                    }
                    else if (GG.Geometry1.Transform is TransformGroup)
                    {
                        if (((TransformGroup)(GG.Geometry1.Transform)).Children.OfType<TranslateTransform>().Count<TranslateTransform>() > 0)
                        {
                            TranslateTransform TT1=((TransformGroup)(GG.Geometry1.Transform)).Children.OfType<TranslateTransform>().First<TranslateTransform>();
                                TT1=new TranslateTransform(((TransformGroup)(GG.Geometry1.Transform)).Children.OfType<TranslateTransform>().First<TranslateTransform>().X+TT.X,
                                    ((TransformGroup)(GG.Geometry1.Transform)).Children.OfType<TranslateTransform>().First<TranslateTransform>().Y+TT.Y);
                        }
                        else
                        {
                            ((TransformGroup)(GG.Geometry1.Transform)).Children.Add(TT);
                        }
                    
                    }
                    else if (GG.Geometry1.Transform is MatrixTransform)
                    {
                        GG.Geometry1.Transform = TT;
                    }
                }
                d1 = Canvas.GetLeft(UnitePaths[1]);
                d2 = Canvas.GetTop(UnitePaths[1]);
                if (!(double.IsNaN(d1)))
                {
                    TranslateTransform TT = new TranslateTransform(d1, d2);
                    if (GG.Geometry2.Transform is TranslateTransform)
                    {
                        ((TranslateTransform)(GG.Geometry2.Transform)).X = ((TranslateTransform)(GG.Geometry2.Transform)).X + d1;
                        ((TranslateTransform)(GG.Geometry2.Transform)).Y = ((TranslateTransform)(GG.Geometry2.Transform)).Y + d2;
                    }
                    else if (GG.Geometry2.Transform is TransformGroup)
                    {
                        if (((TransformGroup)(GG.Geometry2.Transform)).Children.OfType<TranslateTransform>().Count<TranslateTransform>() > 0)
                        {
                            TranslateTransform TT1 = ((TransformGroup)(GG.Geometry2.Transform)).Children.OfType<TranslateTransform>().First<TranslateTransform>();
                            TT1 = new TranslateTransform(((TransformGroup)(GG.Geometry2.Transform)).Children.OfType<TranslateTransform>().First<TranslateTransform>().X + TT.X,
                                ((TransformGroup)(GG.Geometry2.Transform)).Children.OfType<TranslateTransform>().First<TranslateTransform>().Y + TT.Y);
                        }
                        else
                        {
                            ((TransformGroup)(GG.Geometry2.Transform)).Children.Add(TT);
                        }

                    }
                    else if (GG.Geometry2.Transform is MatrixTransform)
                    {
                        GG.Geometry2.Transform = TT;
                    }
                }
                Canvas.SetLeft(UnitePaths[0], 0);
                Canvas.SetTop(UnitePaths[0], 0);
                AktiveCanvas.Children.Remove(UnitePaths[1]);
                UnitePaths.Clear();
                if (ParcaViewBox.Child != null)
                    XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void EscapeKey()
        {
            //uncheck tools
            LineSwitch.IsChecked = false;
            PolyLineSwitch.IsChecked = false;
            ArcSwitch.IsChecked = false;
            BezierSwitch.IsChecked = false;
            QuadraticBezierSwitch.IsChecked = false;
            SplineSwitch.IsChecked = false;
            RectangleSwitch.IsChecked = false;
            ElipseSwitch.IsChecked = false;
            UniteSwitch.IsChecked = false;
            SubstractSwitch.IsChecked = false;
            //remove adorners
            try
            {
                if (SelectedUIElement != null)
                {
                    Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                    if (toRemoveArray != null)
                    {
                        for (int x = 0; x < toRemoveArray.Length; x++)
                        {
                            ADL.Remove(toRemoveArray[x]);
                        }
                    }
                }
            }
            catch { }
            //selected element gets null value
            SelectedUIElement = null;
            PossibleSelectedPaths.Clear();
            UnitePaths.Clear();
            SelectIndex = 0;
            if (FileTree.SelectedItem != null)
            {
                ((TreeViewItem)(FileTree.SelectedItem)).IsSelected = false;
            }
            //reduce Aim
            Aim = "";
            //Clickedpoints Cleared
            ClickedPoints.Clear();
            //Remove trivial Shapes
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        } 

        private void DeleteKey()
        {
            if (SelectedUIElement != null)
            {
                try
                {
                    if (SelectedUIElement != null)
                    {
                        Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                        if (toRemoveArray != null)
                        {
                            for (int x = 0; x < toRemoveArray.Length; x++)
                            {
                                ADL.Remove(toRemoveArray[x]);
                            }
                        }
                    }
                }
                catch { }
                //SelectedUIElement.FindCommonVisualAncestor(SelectedUIElement);
                AktiveCanvas.Children.Remove(SelectedUIElement);
                SelectedUIElement = null;

                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void TabKey()
        {
            if (PossibleSelectedPaths.Count < 1)
                return;
            else
            {
                ++SelectIndex;
                if (SelectIndex > (PossibleSelectedPaths.Count - 1))
                {
                    SelectIndex = 0;
                }
                try
                {
                    if (SelectedUIElement != null)
                    {
                        Adorner[] toRemoveArray = ADL.GetAdorners(SelectedUIElement);
                        if (toRemoveArray != null)
                        {
                            for (int x = 0; x < toRemoveArray.Length; x++)
                            {
                                ADL.Remove(toRemoveArray[x]);
                            }
                        }
                    }
                }
                catch { }
                SelectedUIElement = PossibleSelectedPaths[SelectIndex];

                if (((System.Windows.Shapes.Path)SelectedUIElement).Data is StreamGeometry)
                {
                    ((System.Windows.Shapes.Path)SelectedUIElement).Data = ((System.Windows.Shapes.Path)SelectedUIElement).Data.GetFlattenedPathGeometry().Clone();
                }
                ADL = AdornerLayer.GetAdornerLayer(SelectedUIElement);
                ADL.Add(new XamlEditor.ResizingAdorner((System.Windows.Shapes.Path)SelectedUIElement));            
            }
        }

        private void RemoveTrivialShapes()
        {
            try
            {
                ((Canvas)(RemoveLine.Parent)).Children.Remove(RemoveLine);
            }
            catch { }
            try
            {
                ((Canvas)(RemoveArc.Parent)).Children.Remove(RemoveArc);
            }
            catch { }
            try
            {
                ((Canvas)(RemoveBezier.Parent)).Children.Remove(RemoveBezier);
            }
            catch { }
            try
            {
                ((Canvas)(RemoveQuadraticBezier.Parent)).Children.Remove(RemoveQuadraticBezier);
            }
            catch { }
            try
            {
                ((Canvas)(RemoveSpline.Parent)).Children.Remove(RemoveSpline);
            }
            catch { }
            try
            {
                ((Canvas)(RemoveRectangle.Parent)).Children.Remove(RemoveRectangle);
            }
            catch { }
            try
            {
                ((Canvas)(RemoveEllipse.Parent)).Children.Remove(RemoveEllipse);
            }
            catch { }
        }
     

        //ReadioButtonEvents
        private void LineChecked(object sender, RoutedEventArgs e)
        {
            Aim = "Line";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void EllipseSwitch_Checked(object sender, RoutedEventArgs e)
        {
            Aim = "Ellipse";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void PolyLineSwitch_Checked(object sender, RoutedEventArgs e)
        {
            Aim = "PolyLine";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void RectangleSwitch_Checked(object sender, RoutedEventArgs e)
        {
            Aim = "Rectangle";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void ArcChecked(object sender, RoutedEventArgs e)
        {
            Aim = "Arc";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if(ParcaViewBox.Child!=null)
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void BezierChecked(object sender, RoutedEventArgs e)
        {
            Aim = "Bezier";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void QuadraticBezierChecked(object sender, RoutedEventArgs e)
        {
            Aim = "QuadraticBezier";
            ClickedPoints.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void SplineChecked(object sender, RoutedEventArgs e)
        {
            Aim = "Spline";
            ClickedPoints.Clear();
            Nodes.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void UniteChecked(object sender, RoutedEventArgs e)
        {
            Aim = "Unite";
            ClickedPoints.Clear();
            UnitePaths.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        }

        private void SubstractChecked(object sender, RoutedEventArgs e)
        {
            Aim = "Substract";
            ClickedPoints.Clear();
            UnitePaths.Clear();
            RemoveTrivialShapes();
            if (ParcaViewBox.Child != null)
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);

        }
 

        // Shape Creaters//
        private void CreateEllipse()
        {
            if (ClickedPoints.Count == 2)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.EllipseCreater(ClickedPoints);
                x.Stroke = new SolidColorBrush(AktiveColor);
                AktiveCanvas.Children.Add(x);
                ClickedPoints.Clear();
                AktiveCanvas.Children.Remove(RemoveEllipse);
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void CreateRectangle()
        {
            if (ClickedPoints.Count == 2)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.RectangleCreater(ClickedPoints);
                x.Stroke = new SolidColorBrush(AktiveColor);
                AktiveCanvas.Children.Add(x);
                ClickedPoints.Clear();
                AktiveCanvas.Children.Remove(RemoveRectangle);
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void CreateSpline()
        {
            RemoveTrivialShapes();
            if (ClickedPoints.Count > 1)
            {
                List<PathMaker.Node> A = new List<PathMaker.Node>();
                foreach (Point p in ClickedPoints)
                {
                    A.Add(new PathMaker.Node(NP: p));
                }

                System.Windows.Shapes.Path x = XamlEditor.PathCreator.SplineCreater(ClickedPoints);
                x.Data = PathMaker.Node.GetPath(Nodes);
                x.Stroke = new SolidColorBrush(AktiveColor);
                try
                {
                    AktiveCanvas.Children.Add(x);
                }
                catch { }
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
            if (ClickedPoints.Count > 2)
            {
                List<PathMaker.Node> A = new List<PathMaker.Node>();
                foreach (Point p in ClickedPoints)
                {
                    A.Add(new PathMaker.Node(NP: p));
                }
                AktiveCanvas.Children.RemoveAt(AktiveCanvas.Children.Count - 2);
                AktiveCanvas.Children.RemoveAt(AktiveCanvas.Children.Count - 1);
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.SplineCreater(ClickedPoints);
                x.Data = PathMaker.Node.GetPath(Nodes); 
                x.Stroke = new SolidColorBrush(AktiveColor);
                try
                {
                    AktiveCanvas.Children.Add(x);
                }
                catch { }
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void CreateLine()
        {
            if (ClickedPoints.Count == 2)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.LineCreater(ClickedPoints[0], ClickedPoints[1]);
                x.Stroke = new SolidColorBrush(AktiveColor);
                AktiveCanvas.Children.Add(x);
                ClickedPoints.Clear();
                AktiveCanvas.Children.Remove(RemoveLine);
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void CreatePolyLine()
        {
            if (ClickedPoints.Count > 1)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.PolyLineCreater(ClickedPoints);
                x.Stroke = new SolidColorBrush(AktiveColor);
                try
                {
                    AktiveCanvas.Children.Add(x);
                }
                catch { }
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
            if (ClickedPoints.Count > 2)
            {
                AktiveCanvas.Children.RemoveAt(AktiveCanvas.Children.Count - 2);
                AktiveCanvas.Children.RemoveAt(AktiveCanvas.Children.Count - 1);
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.PolyLineCreater(ClickedPoints);
                x.Stroke = new SolidColorBrush(AktiveColor);
                try 
                {
                    AktiveCanvas.Children.Add(x);
                }
                catch { }
                XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
            }
        }

        private void CreateArc()
        {
            if (ClickedPoints.Count == 3)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.ArcCreater(ClickedPoints);
                AktiveCanvas.Children.Add(x);
                x.Stroke = new SolidColorBrush(AktiveColor);
                ClickedPoints.Clear();
                RemoveTrivialShapes();
            }
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        
        }

        private void CreateBezier()
        {
            if (ClickedPoints.Count == 4)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.BezierCreater(ClickedPoints);
                AktiveCanvas.Children.Add(x);
                x.Stroke = new SolidColorBrush(AktiveColor);
                ClickedPoints.Clear();
                RemoveTrivialShapes();
            }
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        
        }

        private void CreateQuadraticBezier()
        {
            if (ClickedPoints.Count == 3)
            {
                System.Windows.Shapes.Path x = XamlEditor.PathCreator.QuadraticBezierCreater(ClickedPoints);
                AktiveCanvas.Children.Add(x);
                x.Stroke = new SolidColorBrush(AktiveColor);
                ClickedPoints.Clear();
                RemoveTrivialShapes();
            }
            XamlEditor.TreeViewEditor.TreeViewOfCanvas((Canvas)(ParcaViewBox.Child), FileTree);
        
        }
        //AktiveColorEvents
        private void AktiveColorRChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            AktiveColor.R = (byte)e.NewValue;
        }

        private void AktiveColorGChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            AktiveColor.G = (byte)e.NewValue;
        }

        private void AktiveColorBChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            AktiveColor.B = (byte)e.NewValue;
        }

        private void AktiveColorAChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            AktiveColor.A = (byte)e.NewValue;
        }
        
        private void Aktive_Pick_Color_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog CD = new System.Windows.Forms.ColorDialog();
            CD.AllowFullOpen = false;
            CD.AnyColor = true;
            //CD.SolidColorOnly = false;
            //CD.ShowDialog();
            if (CD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                AktiveColorR.Value = CD.Color.R;
                AktiveColorG.Value = CD.Color.G;
                AktiveColorB.Value = CD.Color.B;
            }
        }


        //PathPropertyChangeEvents
        private void ChangePathPropertyClick(object sender, RoutedEventArgs e)
        {
            if (SelectedUIElement == null)
                return;
            else if (SelectedUIElement is System.Windows.Shapes.Path)
            {
                if (Stroke.IsChecked == true)
                {
                    Color StrokeColor = new Color();
                    StrokeColor.A = (byte)StrokeColorA.Value;
                    StrokeColor.R = (byte)StrokeColorR.Value;
                    StrokeColor.G = (byte)StrokeColorG.Value;
                    StrokeColor.B = (byte)StrokeColorB.Value;
                    ((System.Windows.Shapes.Path)(SelectedUIElement)).Stroke = new SolidColorBrush(StrokeColor);
                    try
                    {
                        ((System.Windows.Shapes.Path)(SelectedUIElement)).StrokeThickness = Convert.ToDouble(StrokeThickness.Text);
                    }
                    catch{}
                    try 
                        {
                            if (StrokeDashArray.Text != "0")
                                ((System.Windows.Shapes.Path)(SelectedUIElement)).StrokeDashArray = SP1.AtributeValueLibrary.StrokeDashArrayConvertor(StrokeDashArray.Text);
                            else
                            {
                                ((System.Windows.Shapes.Path)(SelectedUIElement)).StrokeDashArray = null;
                            }
                        }
                    catch { }
                }
                else
                {
                    ((System.Windows.Shapes.Path)(SelectedUIElement)).Stroke = null;
                }
                if (Fill.IsChecked == true)
                {
                    Color FillColor = new Color();
                    FillColor.A = (byte)FillColorA.Value;
                    FillColor.R = (byte)FillColorR.Value;
                    FillColor.G = (byte)FillColorG.Value;
                    FillColor.B = (byte)FillColorB.Value;
                    ((System.Windows.Shapes.Path)(SelectedUIElement)).Fill = new SolidColorBrush(FillColor);
                }
                else
                {
                    ((System.Windows.Shapes.Path)(SelectedUIElement)).Fill = null;
                }
                if (Close_Path.IsChecked == true)
                {
                    try
                    {
                        ((PathGeometry)(((System.Windows.Shapes.Path)(SelectedUIElement)).Data)).Figures[0].IsClosed = true;
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        ((PathGeometry)(((System.Windows.Shapes.Path)(SelectedUIElement)).Data)).Figures[0].IsClosed = false;
                    }
                    catch { }
                }
            }
            else
            {
                return;
            }
        }

        private void Stroke_Pick_Color_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog CD = new System.Windows.Forms.ColorDialog();
            CD.AllowFullOpen = false;
            CD.AnyColor = true;
            //CD.SolidColorOnly = false;
            //CD.ShowDialog();
            if (CD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               StrokeColorR.Value = CD.Color.R;
               StrokeColorG.Value = CD.Color.G;
               StrokeColorB.Value = CD.Color.B;
            }
        }

        private void Fill_Pick_Color_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.ColorDialog CD = new System.Windows.Forms.ColorDialog();
            CD.AllowFullOpen = false;
            CD.AnyColor = true;
            //CD.SolidColorOnly = false;
            //CD.ShowDialog();
            if (CD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FillColorR.Value = CD.Color.R;
                FillColorG.Value = CD.Color.G;
                FillColorB.Value = CD.Color.B;
            }
        }


    }
}
